/**
 *====================================================
 * 文件名称: HttpClientHelper.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2014年6月18日			Administrator(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 * 
 */
package com.yuexunit.finance.common.httpclient;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.yuexunit.jsonutil.JSON;

/**
 * @ClassName: HttpClientHelper
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author Administrator
 * @date 2014年6月18日 下午3:34:28
 */
public class HttpClientHelper {
	
	public static String httpTest(String url,String body,Map<String, String> head)  {  
		HttpPost post=null;
		try {		  
		    HttpClient httpClient = new DefaultHttpClient(); 
		    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		    if (head != null) {
				for (Entry<String, String> entry : head.entrySet()) {
					nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
				}
			}
		    post = new HttpPost(url);  
		    StringEntity postingString = new StringEntity(body,"utf-8");// json传递  
		    post.setEntity(postingString);  
		    post.setHeader("Content-type", "application/json");
		    post.setHeader("X-timevale-mode", "package");  
		    post.setHeader("X-timevale-project-id", head.get("X-timevale-project-id"));  
		    post.setHeader("X-timevale-signature",  head.get("X-timevale-signature")); 
		    post.setHeader("X-timevale-signature-algorithm", "hmac-sha256");  
		    HttpResponse response = httpClient.execute(post);  
		    String content = EntityUtils.toString(response.getEntity());  
		    // Log.i("test",content);  
		    System.out.println(content); 
		    return content;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}finally {
			if (post != null) {
				post.releaseConnection();
			}
		}
	     
	}  
	
	public static String post(String url, Map<String, String> params) {
		HttpPost post = null;
		try {
			CloseableHttpClient httpClient = HttpClients.custom().build();
			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			if (params != null) {
				for (Entry<String, String> entry : params.entrySet()) {
					nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
				}
			}
			post = new HttpPost(url);
			post.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
			HttpResponse response = httpClient.execute(post);
			HttpEntity entity = (HttpEntity) response.getEntity();
			String responseText = EntityUtils.toString(entity, "UTF-8");
			EntityUtils.consume(entity);
			return responseText;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			if (post != null) {
				post.releaseConnection();
			}
		}
	}

	public static String post1(String url, Map<String, List<String>> params) {
		HttpPost post = null;
		try {
			CloseableHttpClient httpClient = HttpClients.custom().build();
			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			if (params != null) {
				for (Entry<String, List<String>> entry : params.entrySet()) {
					for (String str : entry.getValue()) {
						nvps.add(new BasicNameValuePair(entry.getKey(), str));
					}
				}
			}
			post = new HttpPost(url);
			post.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
			HttpResponse response = httpClient.execute(post);
			HttpEntity entity = (HttpEntity) response.getEntity();
			String responseText = EntityUtils.toString(entity, "UTF-8");
			EntityUtils.consume(entity);
			return responseText;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			if (post != null) {
				post.releaseConnection();
			}
		}
	}

	public static String get(String url, Map<String, String> params) {
		HttpGet get = null;
		try {
			CloseableHttpClient httpClient = HttpClients.custom().build();
			if (params != null && !params.isEmpty()) {
				StringBuffer sb = new StringBuffer();
				for (Entry<String, String> entry : params.entrySet()) {
					if (entry.getValue() == null) {
						continue;
					}
					sb.append(entry.getKey());
					sb.append("=");
					sb.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
					sb.append("&");
				}
				if (sb.length() > 0) {
					sb = sb.deleteCharAt(sb.length() - 1);
				}
				url = url + (url.contains("?") ? "&" : "?") + sb;
			}
			get = new HttpGet(url);
			HttpResponse response = httpClient.execute(get);
			HttpEntity entity = (HttpEntity) response.getEntity();
			String responseText = EntityUtils.toString(entity, "UTF-8");
			EntityUtils.consume(entity);
			return responseText;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			if (get != null) {
				get.releaseConnection();
			}
		}
	}

	public static class A {
		private String json;
		private String md5;
		private String resultCode;
		private String resultMessage;
		public String getJson() {
			return json;
		}
		public void setJson(String json) {
			this.json = json;
		}
		public String getMd5() {
			return md5;
		}
		public void setMd5(String md5) {
			this.md5 = md5;
		}
		public String getResultCode() {
			return resultCode;
		}
		public void setResultCode(String resultCode) {
			this.resultCode = resultCode;
		}
		public String getResultMessage() {
			return resultMessage;
		}
		public void setResultMessage(String resultMessage) {
			this.resultMessage = resultMessage;
		}
	}
	
	
	public static void main(String[] args) throws Exception {
//		String url = "http://222.79.247.163:8080/StarChainRemote/servlet/getTraceGoodsList"; vjAAAACEHNXQfqKA
//		String url = "http://api.antong56.com:9090/StarChainRemote/servlet/getTraceGoodsList";
//		Map<String, String> params = new HashMap<>();
//		params.put("userGUID", "vjAAAAEorW/L++se");
//		params.put("userID", "at");
//		params.put("boxNO", "ATLU0003228");
//		params.put("billNO", "ATHKTJ15517260");
//		String responseText = HttpClientHelper.post(url, params);
//		System.out.println(responseText);
//		String url = "http://api.antong56.com:9090/StarChainRemote/servlet/getShipLineList";
//		Map<String, String> params = new HashMap<>();
//		params.put("userID", "test1");
//		params.put("parm", "vjAAAAD3STEfM3Fl");
//		params.put("userGUID", "vjAAAAF4LsnL++se");
//		params.put("type", "6");
//		params.put("deviceID", "860236022034796");
//		String responseText = HttpClientHelper.post(url, params);
//		System.out.println(responseText);
		String url = "http://222.79.247.163:8080/StarChainRemote/servlet/getTallyingsServlet";
		Map<String, String> params = new HashMap<>();
		params.put("billNO", "test-002");
		params.put("boxNO", "YYCU5555555");
		String responseText = HttpClientHelper.post(url, params);
		System.out.println(responseText);
		A bean = JSON.toBean(responseText, A.class);
		System.out.println(URLDecoder.decode(bean.getJson(), "UTF-8"));
		
		

	}

}
